<template>
	<view>
		<button type="primary" class="btn"   @click="dialogToggle(item)" >删除</button>
		
		<button class="button" type="primary" @click="toggle()"><text class="button-text">居中</text></button>
		
		
		<button class="button" type="primary" @click="login()"><text class="button-text">登录</text></button>
		
		{{openid}}
		<button class="button" type="primary" @click="pay()"><text class="button-text">购买</text></button>
		
		<button class="button" type="primary" @click="refund()"><text class="button-text">退款</text></button>
		<!-- 提示窗示例 -->
		<uni-popup ref="alertDialog" type="dialog">
		    <uni-popup-dialog type="warn" cancelText="关闭" confirmText="确认" title="提示" content="确认删除？" @confirm="dialogConfirm" @close="dialogClose">
		    </uni-popup-dialog>
		</uni-popup>
		
		
		<uni-popup ref="popup" background-color="#fff">
			<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
				<image src="../../static/hd1.png" style="width: 500rpx;height: 500rpx;"></image>
				<button type="primary" class="btn"   @click="close()" >删除</button>
			</view>
		</uni-popup>
		
	</view>
</template>

<script setup>
	import http from '../../utils/http';
	import { getCurrentInstance } from 'vue'
	// 获取当前组件实例
	const { ctx } = getCurrentInstance(); 
	
	function dialogToggle(item) {
	    ctx.$refs.alertDialog.open()
	}
	const type=ref('');
	function toggle() {
		// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
		ctx.$refs.popup.open('center')
   }
   function close(){
	   ctx.$refs.popup.close();
   }
   const openid=ref('');
   function login(){
	   wx.login({
	     success (res) {
	       if (res.code) {
	         //发起网络请求
				http.request({
					url: "/user/get_user_info",
					method: "POST",
					data: {
						code:res.code
					}
				}).then((responseData) => {
					 console.log(responseData.data.openid);
					 openid.value=responseData.data.openid;
				}).catch((err) => {
					uni.showToast({
						icon: 'error',
						title: '请求失败'
					})
				})
	       } else {
	         console.log('登录失败！' + res.errMsg)
	       }
	     }
	   })
   }
   
   function pay(){
	   http.request({
	   	url: "/wxPay/pay",
	   	method: "POST",
	   	data: {
	   		openId:openid.value
	   	}
	   }).then((responseData) => {
		   if(responseData.code==1){
			   wx.requestPayment({
			     timeStamp: responseData.data.timeStamp,
			     nonceStr: responseData.data.nonceStr,
			     package: responseData.data.packageVal,
			     signType: 'MD5',
			     paySign: responseData.data.paySign,
			     success (res) { },
			     fail (res) { }
			   })
		   } 
	   }).catch((err) => {
	   	uni.showToast({
	   		icon: 'error',
	   		title: '请求失败'
	   	})
	   })
   }
   
   function refund(){
	   http.request({
	   	url: "/wxPay/refund",
	   	method: "POST",
	   	data: {
	   		openId:openid.value
	   	}
	   }).then((responseData) => {
	   		    
	   }).catch((err) => {
	   	uni.showToast({
	   		icon: 'error',
	   		title: '请求失败'
	   	})
	   })
   }
</script>

<style>
</style>